#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
int main(){
	int head,n,k,dat[100005],next[100005];
	cin >> head >> n >> k;
	int temp;
	for(int i = 0;i < n;i++){
		cin >> temp;
		cin >> dat[temp] >> next[temp];
	}
	int count = 0;
	vector<vector<int> > ans;
	vector<int> row;
	while(head!=-1){
		row.push_back(head);count++;
		if(count==k){
			ans.push_back(row);
			count = 0;
			row.clear();
		}
		head = next[head];
	}
	if(row.size()) ans.push_back(row);
	reverse(ans.begin(),ans.end());
	int flag = 0;
	for(int i = 0;i < ans.size();i++){
		for(int j = 0;j < ans[i].size();j++){
			if(flag==0){
				printf("%05d %d ",ans[i][j],dat[ans[i][j]]);
				flag = 1;
			}
			else printf("%05d\n%05d %d ",ans[i][j],ans[i][j],dat[ans[i][j]]);
		}
	}
	cout << "-1";
	return 0;
}
